package com.zhijiaoyun.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author shisfish
 * @date 2017/6/6
 */
public class DateUtils {
	
	public static Calendar getDate(int day) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_YEAR, day);
		return cal;
	}

	public static Date getDateFormat(String date, String dateFormat) {
		if (StringUtils.isNotEmptyString(date)) {
			SimpleDateFormat sdf;
			if (StringUtils.isNotEmptyString(dateFormat)) {
				sdf = new SimpleDateFormat(dateFormat);
			} else {
				sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			}
			try {
				return sdf.parse(date);
			} catch (ParseException e) {
				e.printStackTrace();
				return null;
			}
		}
		return null;
	}

	public static String getDateFormat(Date date, String dateFormat) {
		SimpleDateFormat sdf;
		if (StringUtils.isNotEmptyString(dateFormat)) {
			sdf = new SimpleDateFormat(dateFormat);
		} else {
			sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		}
		return sdf.format(date);
	}

	public static int getAge(String birthday) {
		return StringUtils.parseInt(getDateFormat(new Date(), "yyyy"), 1994) - StringUtils.parseInt(birthday.split("-")[0], 1994);
	}
	
	
}
